package com.example.su_shi_tan_bas_lib.album.media_reader;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import androidx.annotation.WorkerThread;


import com.example.su_shi_tan_bas_lib.R;
import com.example.su_shi_tan_bas_lib.album.bean.AlbumFile;
import com.example.su_shi_tan_bas_lib.album.bean.AlbumFolder;
import com.example.su_shi_tan_bas_lib.global.AppGlobals;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VideoMediaReader implements AlbumMediaReader {


    /**
     * Video attribute.
     */
    private static final String[] VIDEOS = {
            MediaStore.Video.Media.DATA,
            MediaStore.Video.Media.BUCKET_DISPLAY_NAME,
            MediaStore.Video.Media.MIME_TYPE,
            MediaStore.Video.Media.DATE_ADDED,
            MediaStore.Video.Media.LATITUDE,
            MediaStore.Video.Media.LONGITUDE,
            MediaStore.Video.Media.SIZE,
            MediaStore.Video.Media.DURATION
    };

    @Override
    @WorkerThread
    public void scanMediaFile(Map<String, AlbumFolder> albumFolderMap, AlbumFolder allFileFolder) {
        Context mContext = AppGlobals.getApplication();

        ContentResolver contentResolver = mContext.getContentResolver();
        Cursor cursor = contentResolver.query(
                MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                VIDEOS,
                null,
                null,
                null);

        if (cursor == null) return;
        while (cursor.moveToNext()) {
            String path = cursor.getString(0);
            String bucketName = cursor.getString(1);
            String mimeType = cursor.getString(2);
            long addDate = cursor.getLong(3);
            float latitude = cursor.getFloat(4);
            float longitude = cursor.getFloat(5);
            long size = cursor.getLong(6);
            long duration = cursor.getLong(7);

            AlbumFile videoFile = new AlbumFile();
            videoFile.setMediaType(AlbumFile.TYPE_VIDEO);
            videoFile.setPath(path);
            videoFile.setBucketName(bucketName);
            videoFile.setMimeType(mimeType);
            videoFile.setAddDate(addDate);
            videoFile.setLatitude(latitude);
            videoFile.setLongitude(longitude);
            videoFile.setSize(size);
            videoFile.setDuration(duration);

//                if (mSizeFilter != null && mSizeFilter.filter(size)) {
//                    if (!mFilterVisibility) continue;
//                    videoFile.setDisable(true);
//                }
//                if (mMimeFilter != null && mMimeFilter.filter(mimeType)) {
//                    if (!mFilterVisibility) continue;
//                    videoFile.setDisable(true);
//                }
//                if (mDurationFilter != null && mDurationFilter.filter(duration)) {
//                    if (!mFilterVisibility) continue;
//                    videoFile.setDisable(true);
//                }

            allFileFolder.addAlbumFile(videoFile);
            AlbumFolder albumFolder = albumFolderMap.get(bucketName);

            if (albumFolder != null)
                albumFolder.addAlbumFile(videoFile);
            else {
                albumFolder = new AlbumFolder();
                albumFolder.setName(bucketName);
                albumFolder.addAlbumFile(videoFile);

                albumFolderMap.put(bucketName, albumFolder);
            }
        }
        cursor.close();

    }

    @Override
    @WorkerThread
    public List<AlbumFolder> getAllMediaFile() {
        Map<String, AlbumFolder> albumFolderMap = new HashMap<>();
        AlbumFolder allFileFolder = new AlbumFolder();
        allFileFolder.setChecked(true);
        Context context = AppGlobals.getApplication();
        String name=context.getString(R.string.album_all_videos);
        allFileFolder.setName(name);

        scanMediaFile(albumFolderMap, allFileFolder);

        ArrayList<AlbumFolder> albumFolders = new ArrayList<>();
        Collections.sort(allFileFolder.getAlbumFiles());
        albumFolders.add(allFileFolder);

        for (Map.Entry<String, AlbumFolder> folderEntry : albumFolderMap.entrySet()) {
            AlbumFolder albumFolder = folderEntry.getValue();
            Collections.sort(albumFolder.getAlbumFiles());
            albumFolders.add(albumFolder);
        }
        return albumFolders;
    }
}
